package com.itheima;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Stream 用法 -------> JDK1.8
 */
public class DemoTest01 {

    public static void main(String[] args) {

        List<User> userList = new ArrayList<User>();
        userList.add(new User(1,"Tom","男"));
        userList.add(new User(2,"Rose","女"));
        userList.add(new User(3,"Jack","男"));
        userList.add(new User(4,"JackMa","男"));
        userList.add(new User(5,"Join","男"));
        userList.add(new User(6,"Lee","男"));
        userList.add(new User(7,"Itcast","男"));
        userList.add(new User(8,"Itheima","男"));
        userList.add(new User(9,"Coco","女"));

        //1.输出集合中的每一个用户的用户名
        userList.stream().forEach(user -> {
            System.out.println(user.getName());
        });

        //2.获取集合中所有的用户名, 并将其封装到一个新的集合中
        List<String> nameList = userList.stream().map(user -> {
            return user.getName();
        }).collect(Collectors.toList());
        System.out.println(nameList);


        //3.仅仅获取到性别为女的用户对象 ---> 集合
        List<User> users = userList.stream().filter(user -> {
            return user.getGender().equals("女");
        }).collect(Collectors.toList());
        System.out.println(users);


        //4.根据性别对用户进行分组 , 将性别为女的封装到一个集合中 , 性别为男的封装到另外一个集合中
        Map<String, List<User>> listMap = userList.stream().collect(Collectors.groupingBy(User::getGender));
        System.out.println(listMap); //key - 男 , 女 ----------> value: List<User> ----->

    }
}
